< previous page page_105 next page >

Page 105
The following code segment shows you a couple of simple but useful things that you can do with code in the Load event procedure. One very common use of the Load event procedure is to "preset" the value of one or more properties of controls on your form (or the form itself). Listing 5.2 illustrates how to set the form's Caption property as the form loads to reflect the current user's name (which you can assume has been previously determined elsewhere in the program and stored in the string variable sUserName). You also see how to set the Text property of a text box to contain the current date.
Listing 5.2 TESTLOAD.FRMUsing the Load Event Procedure to Set Properties
Private Sub Form_Load()
    Me.Caption = "Expense Summary for " & sUserName
    txtDate.Text = Date
End Sub
Other common uses of the Load event include setting up connections to data stored in databases, initializing variables, and positioning the form relative to other forms that may be open.
Another form event that you will use often is the Resize event. This event is triggered any time the size of the form is changed, either by the user or by your program. It also occurs after the form's Load event, before the Activate event occurs. Typically, you use the Resize event to change the size of one or more controls on your form. Doing so gives the user more room to work when the size of the form is increased and prevents information from being hidden when the size of the form is decreased. Listing 5.3 shows how the size of a data-bound Grid control changes when the form's size is changed. The code also checks the WindowState property of the form and does not perform the operation when the form is minimized.
Listing 5.3 RESIZE.FRMChanging the Size and Position of Objects When the Form's Size Changes
If Me.WindowState <> vbMinimized And AllowResize Then
    dbgResults.Width = Me.ScaleWidth - 180
    dbgResults.Height = Me.ScaleHeight - dbgResults.Top - 60
End If
The first step in determining the event interaction is to map the order of events in a program. You can easily write a simple program to familiarize yourself with when events occur. First, create a new standard EXE project and place a TextBox control and a command button on it. You can then place code in the control and form event procedures to notify you when an event occurs. The easiest way to do so is to use the Debug.Print statement, which causes text to be displayed in the Immediate window. Simply add Debug.Print statements to the event procedures, such as the following ones for the text box:

 
< previous page page_105 next page >